package com.hanbang.gycatering.property;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.hanbang.gangyu.R;
import com.hanbang.gycatering.adpater.BannerAdAdapter;
import com.hanbang.gycatering.adpater.PropertyAdapter;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.bean.BannerAdData;
import com.hanbang.gycatering.bean.PropertyBeanData;
import com.hanbang.gycatering.classify.ClassifyList;
import com.hanbang.gycatering.homepage.BaseActivity;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.MyToast;
import com.hanbang.gycatering.utils.PagingHelp;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PropertyAnnouncement extends BaseActivity implements
		OnClickListener {
	private ArrayList<BannerAdData> bannerAdDatas = new ArrayList<BannerAdData>();
	private ArrayList<PropertyBeanData> announcementDatas = new ArrayList<PropertyBeanData>();
	private BannerAdAdapter bannerAdAdapter = null;
	private RelativeLayout banner_ad_wygg;
	// 当前top图片
	private int current = 0;
	private PullToRefreshListView palistview;
	private PropertyAdapter myadapter;
	private PagingHelp pagingHelp = new PagingHelp();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.propertyannouncement);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) findViewById(R.id.top_bar_title)).setText("物业公告");
		setBackArrows((ImageView) findViewById(R.id.top_bar_left_image));
		palistview = (PullToRefreshListView) findViewById(R.id.palistivew);
		banner_ad_wygg = (RelativeLayout) findViewById(R.id.banner_ad_wygg);
		bannerAdAdapter = new BannerAdAdapter(
				PropertyAnnouncement.this,
				banner_ad_wygg, bannerAdDatas);
		bannerAdAdapter.setWidthAndHeight(16, 9);
		myadapter = new PropertyAdapter(PropertyAnnouncement.this,
				announcementDatas);
		palistview.setMode(Mode.BOTH);
		palistview.setAdapter(myadapter);
		getAnnouuncementDatas();
		palistview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(PropertyAnnouncement.this,
						AnnouncementDetail.class);
				intent.putExtra("announcementId", announcementDatas.get(position-1).getId());
				startActivity(intent);
			}
		});
		palistview.setOnRefreshListener(new OnRefreshListener2<ListView>() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				// TODO Auto-generated method stub
				bannerAdDatas.clear();
				announcementDatas.clear();
				pagingHelp.clear();
				getAnnouuncementDatas();
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				// TODO Auto-generated method stub
				 
					getAnnouuncementDatasMore();
				  
			}
		});
	}

	private void getAnnouuncementDatas() {
		// TODO Auto-generated method stub
		
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET, HttpInterfaces.noticelist + "&type=2"
						+ "&PageIndex=" + pagingHelp.getPageindex(),
				new MyRequestCallBack<String>(PropertyAnnouncement.this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								pagingHelp.setRecordCount(json
										.getInt("recordCount"));
								pagingHelp.setPageCount(json
										.getInt("pageCount"));
								 
								JSONObject info = json.getJSONArray("info")
										.getJSONObject(0);
								JSONArray img = info.getJSONArray("img");
								for (int i = 0; i < img.length(); i++) {
									BannerAdData hb = new BannerAdData();
									hb.setPath(img.getJSONObject(i).getString(
											"img"));
									bannerAdDatas.add(hb);
								}
								JSONArray notice = json.getJSONArray("info")
										.getJSONObject(1)
										.getJSONArray("notice");
								int j=0;
								for (j= pagingHelp.getCurrentCount(); j < notice.length(); j++) {
									PropertyBeanData pbdata = new PropertyBeanData();
									pbdata.setId(notice.getJSONObject(j)
											.getInt("id"));
									pbdata.setTitle(notice.getJSONObject(j)
											.getString("title"));
									pbdata.setTime(notice.getJSONObject(j)
											.getString("time"));
									announcementDatas.add(pbdata);
								}
								bannerAdAdapter.notifyDataSetChanged();
								bannerAdAdapter.start();
								
								pagingHelp.isNextPaging(PropertyAnnouncement.this,
										notice.length(), j);
							} else {
								pagingHelp.showNoData(PropertyAnnouncement.this);
							}
							myadapter.notifyDataSetChanged();
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(PropertyAnnouncement.this, "获取数据失败",
									Toast.LENGTH_SHORT);
						}
						palistview.onRefreshComplete();
					}
				});
	}

	private void getAnnouuncementDatasMore() {
		// TODO Auto-generated method stub
		
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET, HttpInterfaces.noticelist + "&type=2"
						+ "&PageIndex=" + pagingHelp.getPageindex(),
				new MyRequestCallBack<String>(PropertyAnnouncement.this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								pagingHelp.setRecordCount(json
										.getInt("recordCount"));
								pagingHelp.setPageCount(json
										.getInt("pageCount"));
								JSONArray notice = json.getJSONArray("info")
										.getJSONObject(1)
										.getJSONArray("notice");
								int j=0;
								for (j= pagingHelp.getCurrentCount(); j < notice.length(); j++) {
									PropertyBeanData pbdata = new PropertyBeanData();
									pbdata.setId(notice.getJSONObject(j)
											.getInt("id"));
									pbdata.setTitle(notice.getJSONObject(j)
											.getString("title"));
									pbdata.setTime(notice.getJSONObject(j)
											.getString("time"));
									announcementDatas.add(pbdata);
								}
								pagingHelp.isNextPaging(PropertyAnnouncement.this,
										notice.length(), j);
							} else {
								pagingHelp.showNoData(PropertyAnnouncement.this);
							}
							myadapter.notifyDataSetChanged();
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(PropertyAnnouncement.this, "获取数据失败",
									Toast.LENGTH_SHORT);
						}
						palistview.onRefreshComplete();
					}
				});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {

		case R.id.top_bar_left_image:
			finish();
			break;

		default:
			break;
		}
	}

}
